
import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/index/index.vue'
import Layout from '../layout/Layout'
import Account from '../views/account/account.vue'
import Order from '../views/order/Order.vue'
import Login from '../views/Login.vue'

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Layout",
    component: Layout,
    redirect:'/index',
    children:[{
    path: 'index',
    name: 'Index',
    component: Index
  },{
    path: 'account',
    name: 'Account',
    component: Account
  },{
      path: 'order',
      name: 'Order',
      component: Order
    }]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  }
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
import { getToken } from '../utils/cookieUtils'
import { getUser } from '../api/routerApi'
let whlieArr = ['/login']
router.beforeEach((to, from, next) => {
  let path = to.path

  let token = getToken('token')
  if (token) {
    if (path == '/login') {
      next('/')
    } else {
      getUser({ token: token }).then(res => {
        console.log(res);
      })
    }
  } else {
    if (whlieArr.includes(path)) {
      next()
    } else {
      next('/login')
    }
  }
  next()
})
export default router;
